<!DOCTYPE html>
<html>
<head>
<title>客户列表</title>
<meta charset="utf-8"/>
<link rel="stylesheet" href="../../css/bootstrap.css"/>
<script type="text/javascript" src="../../js/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="../../js/bootstrap.min.js"></script>
<script type="text/javascript" src="../../layer/layer.js"></script>
<script type="text/javascript" src="../../js/sha1.js"></script>
<script type="text/javascript" src="../../js/auth.js"></script>
<script type="text/javascript" src="../../js/CookieShop.js"></script>
</head>
<body>
<div class="container-fluid">
    <nav class="navbar navbar-default" role="navigation">
        <div class="container-fluid">
            <div class="navbar-header">
                <a class="navbar-brand" href="../index.html">蛋糕商城</a>
                <a class="navbar-brand" href="../../pages/index.html">集成后台</a>
            </div>
            <div>
                <ul class="nav navbar-nav">
                    <li ><a href="../../custpages/admin/order_list.html">订单管理</a></li>
                    <li ><a href="../../custpages/admin/user_list.html">客户管理</a></li>
                    <li ><a href="../../custpages/admin/goods_list.html">商品管理</a></li>
                    <li ><a href="../../custpages/admin/type_list.html">类目管理</a></li>
                    <li><a href="javascript:cookieShopLogout()">退出</a></li>
                </ul>
            </div>
        </div>
    </nav>
	<div class="text-right"><a class="btn btn-warning" href="" data-toggle="modal" data-target="#addModal">添加客户</a></div>
	<br>
	<table class="table table-bordered table-hover">
	<tr id="dghead">
		<th width="5%">ID</th>
		<th width="10%">用户名</th>
		<th width="10%">邮箱</th>
		<th width="10%">收件人</th>
		<th width="10%">电话</th>
		<th width="10%">地址</th>
		<th width="12%">操作</th>
	</tr>
	<tbody id="dgcontent">
	</tbody>
</table>
<br>
<div style='text-align:center;'>
    <a class='btn btn-info' id="first" href="javascript:first()">首页</a>
    <a class='btn btn-info' id="prev" href="javascript:prev()">上一页</a>
    <h3 style='display:inline;'>[<span id="currentPage"></span>/<span id="totalPage"></span>]</h3>
    <h3 style='display:inline;'>[<span id="recordsCount"></span>]</h3>
    <a class='btn btn-info' id="next" href="javascript:next()">下一页</a>
    <a class='btn btn-info' id="last" href="javascript:last()">尾页</a>
    <input type='text' class='form-control' id="current" style='display:inline;width:60px;'/><a class='btn btn-info' href='javascript:void(0);' onclick='jump()'>GO</a>
</div>
<br>
</div>
<!-- 模态框 -->
<div class="modal fade" id="addModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <!-- 模态框头部 -->
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">模态标题</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <!-- 模态框主体 -->
      <div class="modal-body">
          <form class="form-horizontal" action="" method="post" id="ff">
    		<div class="form-group">
    			<label for="input_name" class="col-sm-4 control-label">用户名</label>
    			<div class="col-sm-6">
    				<input type="text" class="form-control" id="userName" name="userName" required="required"/>
    			</div>
    		</div>
    		<div class="form-group">
    			<label for="input_name" class="col-sm-4 control-label">邮箱</label>
    			<div class="col-sm-6">
    				<input type="text" class="form-control" id="email" name="email" required="required"/>
    			</div>
    		</div>
    		<div class="form-group">
    			<label for="input_name" class="col-sm-4 control-label">密码</label>
    			<div class="col-sm-6">
    				<input type="password" class="form-control" id="password" name="password" required="required"/>
    			</div>
    		</div>
    		<div class="form-group">
    			<label for="input_name" class="col-sm-4 control-label">确认密码</label>
    			<div class="col-sm-6">
    				<input type="password" class="form-control" id="confirmPassword" name="confirmPassword" required="required"/>
    			</div>
    		</div>
    		<div class="form-group">
    			<label for="input_name" class="col-sm-4 control-label">收货人</label>
    			<div class="col-sm-6">
    				<input type="text" class="form-control" id="name" name="name"/>
    			</div>
    		</div>
    		<div class="form-group">
    			<label for="input_name" class="col-sm-4 control-label">电话</label>
    			<div class="col-sm-6">
    				<input type="text" class="form-control" id="phone" name="phone"/>
    			</div>
    		</div>
    		<div class="form-group">
    			<label for="input_name" class="col-sm-4 control-label">地址</label>
    			<div class="col-sm-6">
    				<input type="text" class="form-control" id="address" name="address"/>
    			</div>
    		</div>
    	</form>
        </div>
      <!-- 模态框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" onclick="addUser()">提交保存</button>
      </div>
    </div>
  </div>
</div>

<!-- 模态框 -->
<div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <!-- 模态框头部 -->
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">模态标题</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <!-- 模态框主体 -->
      <div class="modal-body">
         	<form class="form-horizontal" action="" method="post" id="ffedit">
                <input type="hidden" id="id" name="id"/>
          		<div class="form-group">
         			<label for="input_name" class="col-sm-4 control-label">用户名</label>
         			<div class="col-sm-6">
            				<input type="text" class="form-control" id="userName" name="userName" required="required"/>
         			</div>
          		</div>
          		<div class="form-group">
         			<label for="input_name" class="col-sm-4 control-label">邮箱</label>
         			<div class="col-sm-6">
            				<input type="text" class="form-control" id="email" name="email" required="required""/>
         			</div>
          		</div>
          		<div class="form-group">
         			<label for="input_name" class="col-sm-4 control-label">电话</label>
         			<div class="col-sm-6">
            				<input type="text" class="form-control" id="phone" name="phone"/>
         			</div>
          		</div>
          		<div class="form-group">
         			<label for="input_name" class="col-sm-4 control-label">地址</label>
         			<div class="col-sm-6">
            				<input type="text" class="form-control" id="address" name="address"/>
         			</div>
          		</div>
           	</form>
        </div>
      <!-- 模态框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" onclick="updateUser()">提交保存</button>
      </div>
    </div>
  </div>
</div>

<!-- 模态框 -->
<div class="modal fade" id="changePasswordModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <!-- 模态框头部 -->
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">模态标题</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <!-- 模态框主体 -->
      <div class="modal-body">
         	<form class="form-horizontal" action="" method="post" id="ffchangePassword">
          		<input type="hidden" name="id" id="id">
          		<input type="hidden" name="userName" id="userName">
          		<div class="form-group">
         			<label for="input_name" class="col-sm-4 control-label">用户名</label>
         			<div class="col-sm-5" id="userNameLabel"></div>
          		</div>
          		<div class="form-group">
         			<label for="input_name" class="col-sm-4 control-label">邮箱</label>
         			<div class="col-sm-5" id="emailLabel"></div>
          		</div>
          		<div class="form-group">
         			<label for="input_name" class="col-sm-4 control-label">新密码</label>
         			<div class="col-sm-6">
                        <input type="password" class="form-control" id="password" name="password" required="required">
         			</div>
          		</div>
          		<div class="form-group">
         			<label for="input_name" class="col-sm-4 control-label">确认新密码</label>
         			<div class="col-sm-6">
            				<input type="password" class="form-control" id="confirmPassword" name="confirmPassword" required="required">
         			</div>
          		</div>
           	</form>
        </div>
      <!-- 模态框底部 -->
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" onclick="changePasswordUser()">提交保存</button>
      </div>
    </div>
  </div>
</div>
</body>
<script type="text/javascript">
var mypage = 1;
var myrows = 10;
var currentPage = 1;
var pageCount = 1;
$(document).ready(function(){
  checkAdminAccess();
  setTimeout(function() {
    loadDataGrid(mypage,myrows);
  }, 1000);
});

function loadDataGrid(page,rows){
	$.ajax({
		type:"post",
		url: "../../custcontroller/adminUserCustController/user_list",
		data: {
		    page : page,
			rows : rows,
		},
		dataType: 'json',
		success: function(data, textStatus) {
			debugger;
			if ( data.rows !=null ){
			    $("#dgcontent").html("");
				configPageControls(data.totalPage,data.pageNumber,data.totalCount);
				for (let i=0;i<data.rows.length;i++){
				    appendGridRow(data.rows[i]);
				}
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function appendGridRow(row){
  var rowStr = "<tr id='girdrow'>\n"+
                "<td><p>"+row.id+"</p></td>\n"+
                "<td><p>"+row.userName+"</p></td>\n"+
				"<td><p>"+row.email+"</p></td>\n"+
                "<td><p>"+row.firstName+ " " + row.lastName +"</p></td>\n"+
				"<td><p>"+row.phone+"</p></td>\n"+
				"<td><p>"+row.address+"</p></td>\n"+
				genOperationTd(row) +
                "</tr>\n";
  $("#dgcontent").append(rowStr);
}

function showCoverImage(row){
	if(row.cover){
		return "<img style='height:100px;' border='1' src='data:image/png;base64,"+row.cover+"'/>";
	} else {
		return "<img style='height:100px;' border='1' src='../../images/blank.jpg'/>";
	}
}

function genOperationTd(row){
	var tdStr =	"<td nowrap>\n"+
	            "<a class=\"btn btn-info\" href=\"javascript:findUserByIdForChangePassword("+row.id+")\">重置密码</a>\n"+
				"<a class=\"btn btn-primary\" href=\"javascript:findUserByIdForUpdate("+row.id+")\">修改</a>\n"+
				"<a class=\"btn btn-danger\" href=\"javascript:softDeleteUser("+row.id+")\">软删除</a>"+
				"<a class=\"btn btn-danger\" href=\"javascript:deleteUser("+row.id+")\">删除</a>"+
				"</td>\n";
	return tdStr;
}


function softDeleteUser(id){
	$.ajax({
		type: "post",
		url: "../../controller/userController/softDeleteUser",
		data: {
			"id":id
		},
		dataType: 'json',
		success: function(data, textStatus) {
    		if (data.success == true){
    		    window.location.reload()
    		}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function deleteUser(id){
	$.ajax({
		type: "post",
		url: "../../controller/userController/deleteUser",
		dataType: 'json',
		data:{"id":id},
		success: function(data, textStatus) {
		    if (data.success == true){
    		    window.location.reload()
    		}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function addUser(){
	if ($("#ff").find("#password").val()!=$("#ff").find("#confirmPassword").val()){
		$.messager.alert("错误","密码不匹配！","error");
		return;
	}
	$.ajax({
		type: "post",
		url: "../../controller/userController/addUser",
		data: JSON.stringify({
			userName:$("#ff").find("#userName").val(),
			password:hex_sha1($("#ff").find("#password").val()),
   			active:true,
   			firstName:$("#ff").find("#name").val(),
   			email:$("#ff").find("#email").val(),
   			address:$("#ff").find("#address").val(),
   			phone:$("#ff").find("#phone").val(),
		}),
		dataType: 'json',
		contentType:"application/json;charset=UTF-8",
		success: function(data, textStatus) {
			if (data.success) {
			    layer.msg("用户新增成功", {icon:1,time:3000}, function(){
                  });
                window.location.reload();
			} else {
				$.messager.alert("错误",data.error,"error");
			}
			},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function updateUser(){
	$.ajax({
		type: "post",
		url: "../../custcontroller/adminUserCustController/updateUser",
		data: JSON.stringify({
			id:parseInt($("#ffedit").find("#id").val()),
			userName:$("#ffedit").find("#userName").val(),
			email:$("#ffedit").find("#email").val(),
			address:$("#ffedit").find("#address").val(),
			phone:$("#ffedit").find("#phone").val(),
		}),
		dataType: 'json',
		contentType:"application/json;charset=UTF-8",
		success: function(data, textStatus) {
			if (data.success){
                layer.msg("用户修改成功", {icon:1,time:3000}, function(){
                  });
                window.location.reload();
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function changePasswordUser(){
	var userName = $("#ffchangePassword").find("#userName").val();
	var password = $("#ffchangePassword").find("#password").val();
	var confirmPassword = $("#ffchangePassword").find("#confirmPassword").val();
	if (isBlank(password)||isBlank(confirmPassword)){
	   layer.msg("新密码不可为空！", {icon:2,time:3000}, function(){
              });
		return;
	}
	if (password!=confirmPassword){
	    layer.msg("新密码不匹配！", {icon:2,time:3000}, function(){
			});
		return;
	}
	$.ajax({
		type: "post",
		url: "../../controller/userController/changePasswordUser",
		data: {
			userName:userName,
			password:hex_sha1(password),
		},
		dataType: 'json',
		success: function(data, textStatus) {
			if (data.success) {
			   $("#changePasswordModal").modal("hide");
			   layer.msg("用户密码修改成功", {icon:1,time:3000}, function(){
              });
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function findUserByIdForChangePassword(id){
	$.ajax({
		type:"post",
		url: "../../controller/userController/findUserById",
		data: {
		    id:id
		},
		dataType: 'json',
		success: function(data, textStatus) {
			debugger;
			if ( data.success == true && data.rows !=null ){
     			$("#ffchangePassword").find("#id").val(data.rows.id);
     			$("#ffchangePassword").find("#userName").val(data.rows.userName);
     			$("#ffchangePassword").find("#email").val(data.rows.email);
                $("#ffchangePassword").find("#userNameLabel").text(data.rows.userName);
     			$("#ffchangePassword").find("#emailLabel").text(data.rows.email);
				$("#changePasswordModal").modal("show");
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}

function findUserByIdForUpdate(id){
	$.ajax({
		type:"post",
		url: "../../controller/userController/findUserById",
		data: {
		    id:id
		},
		dataType: 'json',
		success: function(data, textStatus) {
			debugger;
			if ( data.success == true && data.rows !=null ){
     			$("#ffedit").find("#id").val(data.rows.id);
     			$("#ffedit").find("#userName").val(data.rows.userName);
                $("#ffedit").find("#address").val(data.rows.address);
                $("#ffedit").find("#phone").val(data.rows.phone);
     			$("#ffedit").find("#email").val(data.rows.email);
				$("#editModal").modal("show");
			}
		},
		complete : function(XMLHttpRequest, textStatus) {
		},
		error : function(XMLHttpRequest,textStatus,errorThrown) {
			alert("Error:"+textStatus);
			alert(errorThrown.toString());
		}
	});
}
</script>
</html>
